package com.example.selfservicestation.fragment

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.navigation.fragment.findNavController
import com.example.selfservicestation.R
import com.example.selfservicestation.base.BaseBindingFragment
import com.example.selfservicestation.constant.DEFAULT_CARD_BAUD
import com.example.selfservicestation.constant.DEFAULT_CARD_DOOR_BAUD
import com.example.selfservicestation.constant.DEFAULT_CARD_DOOR_PORT
import com.example.selfservicestation.constant.DEFAULT_CARD_PORT
import com.example.selfservicestation.constant.DEFAULT_DOOR_OPEN_TIME
import com.example.selfservicestation.constant.DEFAULT_LOCK_BAUD
import com.example.selfservicestation.constant.DEFAULT_LOCK_CARD
import com.example.selfservicestation.constant.DEFAULT_LOCK_PORT
import com.example.selfservicestation.constant.DEFAULT_LOGIN_CARD
import com.example.selfservicestation.constant.DEFAULT_LOGIN_PHONE
import com.example.selfservicestation.constant.SP_CARD_BAUD
import com.example.selfservicestation.constant.SP_CARD_DOOR_BAUD
import com.example.selfservicestation.constant.SP_CARD_DOOR_PORT
import com.example.selfservicestation.constant.SP_CARD_PORT
import com.example.selfservicestation.constant.SP_DOOR_OPEN_TIME
import com.example.selfservicestation.constant.SP_LOCK_BAUD
import com.example.selfservicestation.constant.SP_LOCK_CARD
import com.example.selfservicestation.constant.SP_LOCK_PORT
import com.example.selfservicestation.constant.SP_LOGIN_CARD
import com.example.selfservicestation.constant.SP_LOGIN_PHONE
import com.example.selfservicestation.databinding.FragmentConfigBinding
import com.example.selfservicestation.event.AudioEvent
import com.example.selfservicestation.event.ConfigChangeEvent
import com.example.selfservicestation.event.ConfigEvent
import com.example.selfservicestation.extensions.safeNavigate
import com.example.selfservicestation.mob.ModBusController
import com.example.selfservicestation.mob.ScannerManager
import com.example.selfservicestation.singleton.SharedPreferenceUtil
import com.example.selfservicestation.util.showToast
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class ConfigFragment: BaseBindingFragment<FragmentConfigBinding>(FragmentConfigBinding::inflate) {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
    }

    private fun initViews(){
        val cardDoorPort = SharedPreferenceUtil.getInt(SP_CARD_DOOR_PORT, DEFAULT_CARD_DOOR_PORT)
        val cardDoorBaud = SharedPreferenceUtil.getInt(SP_CARD_DOOR_BAUD, DEFAULT_CARD_DOOR_BAUD)
        val cardPort = SharedPreferenceUtil.getInt(SP_CARD_PORT, DEFAULT_CARD_PORT)
        val cardBaud = SharedPreferenceUtil.getInt(SP_CARD_BAUD, DEFAULT_CARD_BAUD)
        val lockPort = SharedPreferenceUtil.getInt(SP_LOCK_PORT, DEFAULT_LOCK_PORT)
        val lockBaud = SharedPreferenceUtil.getInt(SP_LOCK_BAUD, DEFAULT_LOCK_BAUD)
        val loginPhone = SharedPreferenceUtil.getString(SP_LOGIN_PHONE, DEFAULT_LOGIN_PHONE)
        val loginCard = SharedPreferenceUtil.getString(SP_LOGIN_CARD, DEFAULT_LOGIN_CARD)
        val lockCard = SharedPreferenceUtil.getString(SP_LOCK_CARD, DEFAULT_LOCK_CARD)
        val doorOpenTime = SharedPreferenceUtil.getString(SP_DOOR_OPEN_TIME, DEFAULT_DOOR_OPEN_TIME)

        binding.etDoorPort.setText(cardDoorPort.toString())
        binding.etDoorBaud.setText(cardDoorBaud.toString())
        binding.etCardPort.setText(cardPort.toString())
        binding.etCardBaud.setText(cardBaud.toString())
        binding.etLockPort.setText(lockPort.toString())
        binding.etLockBaud.setText(lockBaud.toString())
        binding.etPhone.setText(loginPhone)
        binding.etDoorCard.setText(loginCard)
        binding.etLockCard.setText(lockCard)
        binding.etDoorOpenDelay.setText(doorOpenTime)

        binding.btnConfirm.setOnClickListener {
            saveData()
        }

        binding.btnBack.setOnClickListener {
            findNavController().safeNavigate(R.id.configFragment_to_homeFragment)
        }
    }

    private fun saveData(){
        val cardDoorPort = binding.etDoorPort.text.toString()
        val cardDoorBaud = binding.etDoorBaud.text.toString()
        val cardPort = binding.etCardPort.text.toString()
        val cardBaud = binding.etCardBaud.text.toString()
        val lockPort = binding.etLockPort.text.toString()
        val lockBaud = binding.etLockBaud.text.toString()
        val loginPhone = binding.etPhone.text.toString()
        val loginCard = binding.etDoorCard.text.toString()
        val lockCard = binding.etLockCard.text.toString()
        val doorOpenTime = binding.etDoorOpenDelay.text.toString()

        if (cardDoorPort.isBlank() || cardDoorPort.contains(" ")){
            showToast("请输入正确的读卡器串口号")
            return
        }

        if (cardDoorBaud.isBlank() || cardDoorBaud.contains(" ")){
            showToast("请输入正确的读卡器波特率")
            return
        }

        if (cardPort.isBlank() || cardPort.contains(" ")){
            showToast("请输入正确的读卡器串口号")
            return
        }

        if (cardBaud.isBlank() || cardPort.contains(" ")){
            showToast("请输入正确的读卡器波特率")
            return
        }

        if (lockPort.isBlank() || cardPort.contains(" ")){
            showToast("请输入正确的锁控板串口号")
            return
        }

        if (lockBaud.isBlank() || cardPort.contains(" ")){
            showToast("请输入正确的锁控板波特率")
            return
        }

        if (loginPhone.isBlank() || cardPort.contains(" ")){
            showToast("请输入正确的登录手机号")
            return
        }

        if (loginCard.isBlank() || cardPort.contains(" ")){
            showToast("请输入正确的登录卡号")
            return
        }

        if (doorOpenTime.isBlank() || doorOpenTime.contains(" ")){
            showToast("请输入正确的关门延迟时间")
        }
        SharedPreferenceUtil.putInt(SP_CARD_DOOR_PORT,cardDoorPort.toInt())
        SharedPreferenceUtil.putInt(SP_CARD_DOOR_BAUD,cardDoorBaud.toInt())
        SharedPreferenceUtil.putInt(SP_CARD_PORT,cardPort.toInt())
        SharedPreferenceUtil.putInt(SP_LOCK_BAUD,cardBaud.toInt())
        SharedPreferenceUtil.putInt(SP_LOCK_PORT,lockPort.toInt())
        SharedPreferenceUtil.putInt(SP_LOCK_BAUD,lockBaud.toInt())
        SharedPreferenceUtil.putString(SP_LOGIN_PHONE,loginPhone)
        SharedPreferenceUtil.putString(SP_LOGIN_CARD,loginCard)
        SharedPreferenceUtil.putString(SP_LOCK_CARD,lockCard)
        SharedPreferenceUtil.putString(SP_DOOR_OPEN_TIME, doorOpenTime)
        showToast("保存成功")
        EventBus.getDefault().post(ConfigChangeEvent())
        findNavController().safeNavigate(R.id.configFragment_to_homeFragment)
    }
}